<?php

namespace SL\BackBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use SL\FrontBundle\Entity\Agence;
use SL\FrontBundle\Form\AgenceType;

use SL\FrontBundle\Entity\Contact;
use SL\FrontBundle\Form\ContactType;

class InfosController extends Controller
{
	public function AgenceAction()
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Agence');
		
		$page = $repository->findOneBy(array('id' => 1));
		if ($page === null)
		{
			throw $this->createNotFoundException('Erreur dans la base de données, la page Agence est introuvable.');
		}
		
		$form = $this->createForm(new AgenceType, $page);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				$em->persist($page);
				$em->flush();
			}
		}
				
		return $this->render('SLBackBundle:Infos:agence.html.twig', array('form' => $form->createView()));
	}
	
	public function ContactAction()
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Contact');
		
		$page = $repository->findOneBy(array('id' => 1));
		if ($page === null)
		{
			throw $this->createNotFoundException('Erreur dans la base de données, la page Contact est introuvable.');
		}
		
		$form = $this->createForm(new ContactType, $page);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				$em->flush();
			}
		}
				
		return $this->render('SLBackBundle:Infos:contact.html.twig', array('form' => $form->createView()));
	}
}